﻿/// <reference path="../../Plugins/jquery-1.10.2.min.js" />
/// <reference path="../../Plugins/jquery.form.min.js" />

$(document).ready(function () {
    $('#navbar-left li').each(function () {
        $(this).removeClass('active');
        if ($(this).find('i').hasClass('fa-dashboard')) {
            $(this).addClass('active');
        }
    })
})

function AdminListDocument(total, pageSize, pageIndex, order) {

    //Private fileds
    var _pageSize = pageSize;
    var _pageIndex = pageIndex;
    var _total = total;
    var _order = order;
    var _baseUrl = '/api/ApiLibDocuments/GetData';

    var _dataService = new DataService();

    //as
    this.Init = function () {
        Paging();
        DelegateEvent();
    };

    //private function
    function Paging() {
        var totalPages = (_total < pageSize) ? 1 : (Math.floor(_total / _pageSize) + 1);
        if (_total > 0) {
            var options = {
                bootstrapMajorVersion: 3,
                currentPage: _pageIndex,
                totalPages: totalPages,
                size: 'normal',
                useBootstrapTooltip: true,
                tooltipTitles: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "Về trang đầu";
                        case "prev":
                            return "Trang trước";
                        case "next":
                            return "Trang tiếp";
                        case "last":
                            return "Về trang cuối";
                        case "page":
                            return "Trang " + page;
                    }
                },
                onPageClicked: function (e, originalEvent, type, page) {
                    var url = _baseUrl + '?index=' + page + '&size=' + _pageSize + '&order=' + _order;

                    _dataService.Get(url, successCallback);
                }
            };
            $('#paging').bootstrapPaginator(options);
        }
    }

    function successCallback(data, textStatus, jqXHR) {
        InsertDocumentItem(data);
        if (data.total != _total) {
            var newTotal = (data.total < pageSize) ? 1 : Math.floor(data.total / _pageSize);
            var newOptions = {
                totalPages: newTotal
            };
            $('#paging').bootstrapPaginator(newOptions);
        }
    }

    function InsertDocumentItem(data) {
        if (!data) {
            return;
        }
        $('#tableData>tbody').empty();
        for (var i = 0; i < data.data.length; i++) {
            var html = '<tr>' +
                            '<td>' + (i + 1).toString() + '</td>' +
                            '<td>' + ((data.data[i].DocumentName != null) ? data.data[i].DocumentName : '') + '</td>' +
                            '<td>' + ((data.data[i].CreateDate != null) ? new Date(data.data[i].CreateDate).format('dd/mm/yyyy') : '01/01/2013') + '</td>' +
                            '<td>' + ((data.data[i].CategoryName != null) ? data.data[i].CategoryName : '') + '</td>' +
                            '<td>' + ((data.data[i].Author != null) ? data.data[i].Author : '') + '</td>' +
                            '<td>' + ((data.data[i].Total != null) ? data.data[i].Total : '') + '</td>' +
                            '<td>' + ((data.data[i].ViewCount != null) ? data.data[i].ViewCount : '') + '</td>' +
                            '<td>' +
                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data[i].DocumentId != null) ? data.data[i].DocumentId : '#') + '"><i class="fa fa-info-circle"></i></button>' +
                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data[i].DocumentId != null) ? data.data[i].DocumentId : '#') + '"><i class="fa fa-trash-o"></i></button>' +
                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data[i].DocumentId != null) ? data.data[i].DocumentId : '#') + '"><i class="fa fa-upload"></i></button>' +
                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data[i].DocumentId != null) ? data.data[i].DocumentId : '#') + '"><i class="fa fa-credit-card"></i></button>' +
                            '</td>' +
                       '</tr>';
            $('#tableData>tbody').append(html);
        }
    }

    function DelegateEvent() {
        //Edit
        $('#tableData>tbody').delegate('td>button', 'click', function (e) {
            e.preventDefault();
            var self = $(this);
            var pos = self.closest('tr').index();
            if (pos != null) {
                if (self.find('i').hasClass('fa-info-circle')) {
                    //Set data to modal
                    _dataService.Get('/api/ApiLibDocuments/GetById?documentId=' + self.attr('value'), function (data, textStatus, jqXHR) {
                        //console.log(data.data);
                        var categoryId = data.data.CategoryId;
                        var rackId = data.data.RackId;
                        $('#mdocumentName').val(data.data.DocumentName);
                        //$('#mkyHieu').val(data.data.KyHieu);
                        $('#mdocumentAuthor').val(data.data.Author);
                        $('#mdocumentId').val(data.data.DocumentId);
                        $('#mdocumentTotal').val(data.data.Total);
                        $('#mdocumentNXB').val(data.data.NXB);
                        $('#mdocumentViewCount').val(data.data.ViewCount);
                        $('#mdocumentDescription').val(data.data.Descriptions);
                        $('#documentThumbnail .fileinput-new.thumbnail').empty();
                        $('#documentThumbnail .fileinput-exists img').remove();
                        $('.fileinput').fileinput('clear');
                        $('#documentThumbnail .fileinput-new.thumbnail').append('<img src="' + ((data.data.ThumbnaiPath == null) ? '' : '../../Contents/Image/DocumentThumbnails/' + data.data.ThumbnaiPath) + '">');
                        //Add Categories
                        _dataService.Get('/api/ApiLibCategories/GetAll', function (data, textStatus, jqXHR) {
                            if (data.success == true) {
                                $('#mcategoryName').empty();
                                for (var category = 0; category < data.data.length; category++) {
                                    var html = '<option value="' + data.data[category].CategoryId + '">' + data.data[category].CategoryName + '</option>';
                                    $('#mcategoryName').append(html);
                                }
                                if ($("#mcategoryName option[value='" + categoryId + "']").length == 1) {
                                    $('#mcategoryName').val(categoryId);
                                }
                            }
                        });
                        //Add Rack
                        _dataService.Get('/api/ApiLibRack/GetAll', function (data, textStatus, jqXHR) {
                            if (data.success == true) {
                                $('#mdocumentRack').empty();
                                for (var rack = 0; rack < data.data.length; rack++) {
                                    var html = '<option value="' + data.data[rack].RackId + '">' + data.data[rack].RackName + '</option>';
                                    $('#mdocumentRack').append(html);
                                }
                                $('#mdocumentRack').prepend('<option value=""></option>');
                                if ($("#mdocumentRack option[value='" + rackId + "']").length == 1) {
                                    $('#mdocumentRack').val(rackId);
                                }
                                else {
                                    $('#mdocumentRack').val(0);
                                }
                            }
                        });
                    });

                    $('#myModalFooter .btn-primary').attr('value', 'edit');
                    $('#myModalLabel').text('DocumentId: ' + self.attr('value'));
                    $('#myModal').modal('show');
                }
                else if (self.find('i').hasClass('fa-trash-o')) {
                    var documentId = self.attr('value');
                    if (confirm('Tiếp tục xóa tài liệu này ?')) {
                        _dataService.Post('/api/ApiLibDocuments/DeleteData?DocumentId=' + documentId, null, function (data, textStatus, jqXHR) {
                            if (data.success == true) {
                                self.closest('tr').remove();
                            }
                        });
                    }
                }
                else if (self.find('i').hasClass('fa-upload')) {
                    var url = '/documents-upload/' + self.attr('value') + '/false';
                    $('#uploadModal').modal({
                        backdrop: true,
                        keyboard: false
                    });
                    $('#uploadModalBody').load(url);
                    $('#uploadModal').modal('show');
                }
            }
        });

        //Addnew
        $('#addNew').click(function () {
            //Add Categories
            _dataService.Get('/api/ApiLibCategories/GetAll', function (data, textStatus, jqXHR) {
                if (data.success == true) {
                    $('#mcategoryName').empty();
                    for (var category = 0; category < data.data.length; category++) {
                        var html = '<option value="' + data.data[category].CategoryId + '">' + data.data[category].CategoryName + '</option>';
                        $('#mcategoryName').append(html);
                    }
                }
            });
            //Add Rack
            _dataService.Get('/api/ApiLibRack/GetAll', function (data, textStatus, jqXHR) {
                if (data.success == true) {
                    $('#mdocumentRack').empty();
                    for (var rack = 0; rack < data.data.length; rack++) {
                        var html = '<option value="' + data.data[rack].RackId + '">' + data.data[rack].RackName + '</option>';
                        $('#mdocumentRack').append(html);
                    }
                    $('#mdocumentRack').prepend('<option value=""></option>');
                }
            });
            $('#mdocumentName').val('');
            $('#mkyHieu').val('');
            $('#mdocumentAuthor').val('');
            $('#mdocumentId').val('');
            $('#mdocumentTotal').val('');
            $('#mdocumentNXB').val('');
            $('#mdocumentViewCount').val('');
            $('#mdocumentDescription').val('');
            $('#documentThumbnail .fileinput-new.thumbnail img').remove();
            $('#documentThumbnail .fileinput-exists img').remove();
            $('.fileinput').fileinput('clear');
            $('#myModal-alerts').empty();
            $('#mdocumentName').closest('.form-group').removeClass('has-error');
            $('#mdocumentTotal').closest('.form-group').removeClass('has-error');
            $('#myModalFooter>.btn-primary').attr('value', 'addnew');
            $('#myModalLabel').text('Thêm mới tài liệu');
            $('#myModal').modal('show');
        });

        //Save
        $('#myModalFooter .btn-primary').click(function () {
            var self = $(this);
            if (self.attr('value') == 'addnew') {
                if (!Validate()) {
                    return;
                }
                if ($('#documentThumbnail input[type="file"]').prop('files')[0] != null) {                        
                    var filename = $('#documentThumbnail input[type="file"]').prop('files')[0].name;//Get thumbnail image name
                    var option = {
                        url: '/api/ApiLibDocuments/UploadThumbnail?filename=' + filename,
                        success: function (responseText, statusText, xhr, $forms) {
                            if (responseText.success == true) {//uploaded thumbnail
                                var data = {
                                    Author: ($('#mdocumentAuthor').val() == '' ? null : $('#mdocumentAuthor').val()),
                                    CategoryId: ($('#mcategoryName').val() == '' ? null : $('#mcategoryName').val()),
                                    Descriptions: ($('#mdocumentDescription').val() == '' ? null : $('#mdocumentDescription').val()),
                                    DocumentName: ($('#mdocumentName').val() == '' ? null : $('#mdocumentName').val()),
                                    KyHieu: ($('#mkyHieu').val() == '' ? null : $('#mkyHieu').val()),
                                    NXB: ($('#mdocumentNXB').val() == '' ? null : $('#mdocumentNXB').val()),
                                    RackId: ($('#mdocumentRack').val() == '' ? null : $('#mdocumentRack').val()),
                                    ThumbnaiPath: responseText.message.split('\\').pop(),
                                    Total: ($('#mdocumentTotal').val() == '' ? null : $('#mdocumentTotal').val())
                                };
                                //Begin create document
                                _dataService.Post('/api/ApiLibDocuments/CreateData', data, function (data, textStatus, jqXHR) {
                                    if (data.success) {
                                        var html = '<tr>' +
                                                        '<td>#</td>' +
                                                        '<td>' + ((data.data.DocumentName != null) ? data.data.DocumentName : '') + '</td>' +
                                                        '<td>' + ((data.data.CreateDate != null) ? new Date(data.data.CreateDate).format('dd/mm/yyyy') : '01/01/2013') + '</td>' +
                                                        '<td>' + ((data.data.CategoryId != null) ? $('#mcategoryName option[value="' + data.data.CategoryId + '"]').text() : '') + '</td>' +
                                                        '<td>' + ((data.data.Author != null) ? data.data.Author : '') + '</td>' +
                                                        '<td>' + ((data.data.Total != null) ? data.data.Total : '') + '</td>' +
                                                        '<td>' + ((data.data.ViewCount != null) ? data.data.ViewCount : '') + '</td>' +
                                                        '<td>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-info-circle"></i></button>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-trash-o"></i></button>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-upload"></i></button>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-credit-card"></i></button>' +
                                                        '</td>' +
                                                    '</tr>';
                                        $('#tableData>tbody').prepend(html);
                                        $('#myModal').modal('hide');
                                    }
                                });
                            }
                            else {
                                var data = {
                                    Author: ($('#mdocumentAuthor').val() == '' ? null : $('#mdocumentAuthor').val()),
                                    CategoryId: ($('#mcategoryName').val() == '' ? null : $('#mcategoryName').val()),
                                    Descriptions: ($('#mdocumentDescription').val() == '' ? null : $('#mdocumentDescription').val()),
                                    DocumentName: ($('#mdocumentName').val() == '' ? null : $('#mdocumentName').val()),
                                    KyHieu: ($('#mkyHieu').val() == '' ? null : $('#mkyHieu').val()),
                                    NXB: ($('#mdocumentNXB').val() == '' ? null : $('#mdocumentNXB').val()),
                                    RackId: ($('#mdocumentRack').val() == '' ? null : $('#mdocumentRack').val()),
                                    Total: ($('#mdocumentTotal').val() == '' ? null : $('#mdocumentTotal').val())
                                };

                                _dataService.Post('/api/ApiLibDocuments/CreateData', data, function (data, textStatus, jqXHR) {
                                    if (data.success) {
                                        var html = '<tr>' +
                                                        '<td>#</td>' +
                                                        '<td>' + ((data.data.DocumentName != null) ? data.data.DocumentName : '') + '</td>' +
                                                        '<td>' + ((data.data.CreateDate != null) ? new Date(data.data.CreateDate).format('dd/mm/yyyy') : '01/01/2013') + '</td>' +
                                                        '<td>' + ((data.data.CategoryId != null) ? $('#mcategoryName option[value="' + data.data.CategoryId + '"]').text() : '') + '</td>' +
                                                        '<td>' + ((data.data.Author != null) ? data.data.Author : '') + '</td>' +
                                                        '<td>' + ((data.data.Total != null) ? data.data.Total : '') + '</td>' +
                                                        '<td>' + ((data.data.ViewCount != null) ? data.data.ViewCount : '') + '</td>' +
                                                        '<td>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-info-circle"></i></button>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-trash-o"></i></button>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-upload"></i></button>' +
                                                            '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-credit-card"></i></button>' +
                                                        '</td>' +
                                                    '</tr>';
                                        $('#tableData>tbody').prepend(html);
                                        $('#myModal').modal('hide');
                                    }
                                });
                            }
                        }
                    };
                    //Begin request
                    $('#documentThumbnail').ajaxSubmit(option);
                }
                else {
                    var data = {
                        Author: ($('#mdocumentAuthor').val() == '' ? null : $('#mdocumentAuthor').val()),
                        CategoryId: ($('#mcategoryName').val() == '' ? null : $('#mcategoryName').val()),
                        Descriptions: ($('#mdocumentDescription').val() == '' ? null : $('#mdocumentDescription').val()),
                        DocumentName: ($('#mdocumentName').val() == '' ? null : $('#mdocumentName').val()),
                        KyHieu: ($('#mkyHieu').val() == '' ? null : $('#mkyHieu').val()),
                        NXB: ($('#mdocumentNXB').val() == '' ? null : $('#mdocumentNXB').val()),
                        RackId: ($('#mdocumentRack').val() == '' ? null : $('#mdocumentRack').val()),
                        Total: ($('#mdocumentTotal').val() == '' ? null : $('#mdocumentTotal').val())
                    };

                    _dataService.Post('/api/ApiLibDocuments/CreateData', data, function (data, textStatus, jqXHR) {
                        if (data.success) {
                            var html = '<tr>' +
                                            '<td>#</td>' +
                                            '<td>' + ((data.data.DocumentName != null) ? data.data.DocumentName : '') + '</td>' +
                                            '<td>' + ((data.data.CreateDate != null) ? new Date(data.data.CreateDate).format('dd/mm/yyyy') : '01/01/2013') + '</td>' +
                                            '<td>' + ((data.data.CategoryId != null) ? $('#mcategoryName option[value="' + data.data.CategoryId + '"]').text() : '') + '</td>' +
                                            '<td>' + ((data.data.Author != null) ? data.data.Author : '') + '</td>' +
                                            '<td>' + ((data.data.Total != null) ? data.data.Total : '') + '</td>' +
                                            '<td>' + ((data.data.ViewCount != null) ? data.data.ViewCount : '') + '</td>' +
                                            '<td>' +
                                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-info-circle"></i></button>' +
                                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-trash-o"></i></button>' +
                                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-upload"></i></button>' +
                                                '<button class="btn btn-default btn-xs table-action" style="margin-right:7px;" value="' + ((data.data.DocumentId != null) ? data.data.DocumentId : '#') + '"><i class="fa fa-credit-card"></i></button>' +
                                            '</td>' +
                                        '</tr>';
                            $('#tableData>tbody').prepend(html);
                            $('#myModal').modal('hide');
                        }
                    });
                }
            }
            else if (self.attr('value') == 'edit') {
                if (!Validate()) {
                    return;
                }
                if ($('#documentThumbnail input[type="file"]').prop('files')[0] != null) {
                    var filename = $('#documentThumbnail input[type="file"]').prop('files')[0].name;//Get thumbnail image name
                    var option = {
                        url: '/api/ApiLibDocuments/UploadThumbnail?filename=' + filename,
                        success: function (responseText, statusText, xhr, $forms) {
                            if (responseText.success == true) {//uploaded thumbnail
                                var data = {
                                    Author: ($('#mdocumentAuthor').val() == '' ? null : $('#mdocumentAuthor').val()),
                                    CategoryId: ($('#mcategoryName').val() == '' ? null : $('#mcategoryName').val()),
                                    Descriptions: ($('#mdocumentDescription').val() == '' ? null : $('#mdocumentDescription').val()),
                                    DocumentId: ($('#mdocumentId').val() == '' ? null : $('#mdocumentId').val()),
                                    DocumentName: ($('#mdocumentName').val() == '' ? null : $('#mdocumentName').val()),
                                    KyHieu: ($('#mkyHieu').val() == '' ? null : $('#mkyHieu').val()),
                                    NXB: ($('#mdocumentNXB').val() == '' ? null : $('#mdocumentNXB').val()),
                                    RackId: ($('#mdocumentRack').val() == '' ? null : $('#mdocumentRack').val()),
                                    ThumbnaiPath: responseText.message.split('\\').pop(),
                                    Total: ($('#mdocumentTotal').val() == '' ? null : $('#mdocumentTotal').val())
                                };
                                _dataService.Post('/api/ApiLibDocuments/UpdateData', data, function (data, textStatus, jqXHR) {
                                    if (data.success) {
                                        $('#tableData button[value="' + $('#mdocumentId').val() + '"]:first').closest('tr').find('td').each(function (e, t) {
                                            switch (e) {
                                                case 1: $(this).text(data.data.DocumentName);
                                                    break;
                                                case 3: $(this).text($('#mcategoryName option[value="' + data.data.CategoryId + '"]').text());
                                                    break;
                                                case 4: $(this).text((data.data.Author == null) ? '' : data.data.Author == null);
                                                    break;
                                                case 5: $(this).text(data.data.Total);
                                                    break;
                                            }
                                        });
                                        $('#myModal').modal('hide');
                                    }
                                });
                            }
                            else {
                                var data = {
                                    Author: ($('#mdocumentAuthor').val() == '' ? null : $('#mdocumentAuthor').val()),
                                    CategoryId: ($('#mcategoryName').val() == '' ? null : $('#mcategoryName').val()),
                                    Descriptions: ($('#mdocumentDescription').val() == '' ? null : $('#mdocumentDescription').val()),
                                    DocumentId: ($('#mdocumentId').val() == '' ? null : $('#mdocumentId').val()),
                                    DocumentName: ($('#mdocumentName').val() == '' ? null : $('#mdocumentName').val()),
                                    KyHieu: ($('#mkyHieu').val() == '' ? null : $('#mkyHieu').val()),
                                    NXB: ($('#mdocumentNXB').val() == '' ? null : $('#mdocumentNXB').val()),
                                    RackId: ($('#mdocumentRack').val() == '' ? null : $('#mdocumentRack').val()),
                                    Total: ($('#mdocumentTotal').val() == '' ? null : $('#mdocumentTotal').val())
                                };
                                _dataService.Post('/api/ApiLibDocuments/UpdateData', data, function (data, textStatus, jqXHR) {
                                    if (data.success) {
                                        $('#tableData button[value="' + $('#mdocumentId').val() + '"]:first').closest('tr').find('td').each(function (e, t) {
                                            switch (e) {
                                                case 1: $(this).text(data.data.DocumentName);
                                                    break;
                                                case 3: $(this).text($('#mcategoryName option[value="' + data.data.CategoryId + '"]').text());
                                                    break;
                                                case 4: $(this).text((data.data.Author == null) ? '' : data.data.Author == null);
                                                    break;
                                                case 5: $(this).text(data.data.Total);
                                                    break;
                                            }
                                        });
                                        $('#myModal').modal('hide');
                                    }
                                });
                            }
                        }
                    };
                    //Begin request
                    $('#documentThumbnail').ajaxSubmit(option);
                }
                else {
                    var data = {
                        Author: ($('#mdocumentAuthor').val() == '' ? null : $('#mdocumentAuthor').val()),
                        CategoryId: ($('#mcategoryName').val() == '' ? null : $('#mcategoryName').val()),
                        Descriptions: ($('#mdocumentDescription').val() == '' ? null : $('#mdocumentDescription').val()),
                        DocumentId: ($('#mdocumentId').val() == '' ? null : $('#mdocumentId').val()),
                        DocumentName: ($('#mdocumentName').val() == '' ? null : $('#mdocumentName').val()),
                        KyHieu: ($('#mkyHieu').val() == '' ? null : $('#mkyHieu').val()),
                        NXB: ($('#mdocumentNXB').val() == '' ? null : $('#mdocumentNXB').val()),
                        RackId: ($('#mdocumentRack').val() == '' ? null : $('#mdocumentRack').val()),
                        Total: ($('#mdocumentTotal').val() == '' ? null : $('#mdocumentTotal').val())
                    };

                    _dataService.Post('/api/ApiLibDocuments/UpdateData', data, function (data, textStatus, jqXHR) {
                        if (data.success) {
                            $('#tableData button[value="' + $('#mdocumentId').val() + '"]:first').closest('tr').find('td').each(function (e, t) {
                                switch (e) {
                                    case 1: $(this).text(data.data.DocumentName);
                                        break;
                                    case 3: $(this).text($('#mcategoryName option[value="' + data.data.CategoryId + '"]').text());
                                        break;
                                    case 4: $(this).text((data.data.Author == null) ? '' : data.data.Author == null);
                                        break;
                                    case 5: $(this).text(data.data.Total);
                                        break;
                                }
                            });
                            $('#myModal').modal('hide');
                        }
                    });
                }
            }
        });
    }

    function Validate() {
        $('#myModal-alerts').empty();
        $('#mdocumentName').closest('.form-group').removeClass('has-error');
        $('#mdocumentTotal').closest('.form-group').removeClass('has-error');
        var isError = false;
        var errCount = 0;
        var errMessage = '';
        var mdocumentName = $('#mdocumentName').val();
        var mdocumentTotal = $('#mdocumentTotal').val();
        if (mdocumentName == null || mdocumentName.trim().length === 0) {
            isError = true;
            errCount++;
            errMessage += '<p>' + errCount + '. Nhập tên tài liệu.</p>';
            $('#mdocumentName').closest('.form-group').addClass('has-error');
        }
        else if (mdocumentTotal == null || isNaN(mdocumentTotal) || mdocumentTotal.trim().length === 0 || mdocumentTotal < 0) {
            isError = true;
            errCount++;
            errMessage += '<p>' + errCount + '. Nhập số lượng tài liệu.</p>';
            $('#mdocumentTotal').closest('.form-group').addClass('has-error');
        }

        //Show alert        
        var alertError = '<div class="alert alert-danger alert-dismissable">' +
                             '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' +
                             '<strong>Có ' + errCount + ' lỗi nhập liệu.</strong> Xin hãy nhập lại.' +
                             '<p>Thông tin lỗi:</p>' +
                             errMessage +
                         '</div>';
        $('#myModal-alerts').prepend(alertError);
        return !isError;
    }
}